<template>
  <div class="page-modal">
    <el-dialog
      title="新建用户"
      v-model="isShowDialog"
      width="30%"
      center
      destroy-on-close
    >
      <TForm v-bind="modalConfig" v-model="formData"></TForm>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="isShowDialog = false">取 消</el-button>
          <el-button type="primary" @click="confirmClick">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import TForm from '@/base-ui/from'
import store from '@/store'
import { ref, watch } from 'vue'

const props = defineProps({
  modalConfig: {
    type: Object,
    required: true
  },
  defaultInfo: {
    type: Object,
    default: () => ({})
  },
  pageName: {
    type: String,
    required: true
  },
  otherInfo: {
    type: Object,
    default: () => ({})
  }
})

const formData = ref<any>({})
const isShowDialog = ref(false)

watch(
  () => props.defaultInfo,
  (newValue) => {
    for (const item of props.modalConfig.formItems) {
      formData.value[`${item.field}`] = newValue[`${item.field}`]
    }
  }
)

const confirmClick = () => {
  isShowDialog.value = false
  if (Object.keys(props.defaultInfo).length) {
    store.dispatch('system/editPageDataAction', {
      pageName: props.pageName,
      editData: { ...props.defaultInfo, ...props.otherInfo },
      id: props.defaultInfo.id
    })
  } else {
    store.dispatch('system/createPageDataAction', {
      pageName: props.pageName,
      newData: { ...formData.value, ...props.otherInfo }
    })
  }
}

defineExpose({
  isShowDialog
})
</script>
<style lang="less" scoped></style>
